package hyl.core.info;

import java.util.ArrayList;
import java.util.List;

//适用于1t以下的文件
/**
 * 
 * <p>
 * 
 * @className: FileInfo
 *             </p>
 *             <p>
 *             Description: 文件信息类 适用于 断点续传等需要解析文件信息 ,分块存储的环境
 *             </p>
 * 
 * @author: zoudyaou qq:37798955
 * @date: 2018年11月13日 下午2:53:52
 * @copyright: 2018 zoudayou Inc. All rights reserved.
 *             注意：代码版权归邹达有所有，任何人未经授权不得私自用于商业目的
 */
public class FileInfo {
	long filesize = 0L;
	long blocksize = 1024 * 512; // 512k
	String fileID = null;
	int filecount = 0;
	String token = "";
	List<FileBlock> fileparts = new ArrayList<FileBlock>();

	public void ini(String fileID, long filesize, int blocksize) {
		this.fileID = fileID;
		if (filesize < blocksize) {
			this.blocksize = (int) filesize;
		}
		if (blocksize > this.blocksize)
			this.blocksize = blocksize;
		this.filesize = filesize;
		this.filecount = (int) (filesize / blocksize) + (filesize % blocksize == 0 ? 0 : 1);
		if (filecount == 0) {
			FileBlock fp = new FileBlock(0, (int) filesize, 0L);
			fileparts.add(fp);
			return;
		}
		for (int i = 0; i < filecount; i++) {
			long sk = i * blocksize;
			long size = (i < filecount - 1) ? blocksize : filesize % blocksize;
			FileBlock fp = new FileBlock(i, size, sk);
			fileparts.add(fp);
		}
	}

	// 取某一块 摘要制作成md5进行验证
	public boolean checkPartMD5(int blockindex, String md5) {
		return fileparts.get(blockindex).checkMd5(md5);
	}

	// 取某一块 摘要制作成md5
	public void SetPartMD5(int blockindex, String md5) {
		fileparts.get(blockindex).setMd5(md5);
	}

	// 验证摘要长度
	public boolean checkPartSize(int blockindex, long size) {
		return fileparts.get(blockindex).checksize(size);
	}
}
